*****************************************************************
*********** Mining and violent competition in Africa ************
****** A.S. Rigterink, T. Ghani, J.S. Lozano, J.N. Shapiro ******
***************** Generating all tables *************************
/*
0. Setting directory globals
1. Setting table globals
2. Retrieving results 
3. Labelling
4. Creating tables for main text 
5. Creating tables for appendix
6. Creating figures
7. Descriptive statistics 
8. Table D10

*/
*****************************************************************

clear all
version 14.0
set more off
cap log close


/*--------------------------------
------- 0. Setting globals -------
----------------------------------*/

*** Directory globals
*** NB: SET YOUR DIRECTORY HERE AND UNTEXT IF NOT RUNNING 0-Master.do
*global dir = ".\Replication_data\Stata"
*cd "$dir"

*** Subdirectories
global Do_files     "./Do"
global Data			"./Data"
global Results		"./Results"
global Tables 		"./Tables"
global Figures		"./Figures"

*** Scripts for spatially clustered standard errors
do "$Do_files/my_ols_spatial_HAC.do"
do "$Do_files/my_reg2hdfespatial.ado"

/*---------------------------------------
------- 1. Setting table globals  -------
-----------------------------------------*/

* Standard table design
global tabledesign "replace se label s(countryyear_FE grid_FE  controlmean effectsize sample N, label("Country-year FEs" "Grid FEs"  "Control mean" "Total stand. effect" "Sample" "Observations" ) fmt(%3s %3s %15.3f %15.3f %100s %15.0f)) b(%15.3f) starlevels(* 0.1 ** 0.05 *** 0.01) nonotes" 

global tabledesign_noeff "replace se label s(countryyear_FE grid_FE controlmean sample N, label("Country-year FEs" "Grid FEs"  "Control mean" "Sample" "Observations" ) fmt(%3s %3s %15.3f %100s %15.0f)) b(%15.3f) starlevels(* 0.1 ** 0.05 *** 0.01) nonotes" 

global tabledesign_actual "replace se label s(countryyear_FE grid_FE country_FE year_FE controlmean sample N, label("Country-year FEs" "Grid FEs" "Country FEs" "Year FEs" "Control mean" "Sample" "Observations" ) fmt(%3s %3s %15.3f %15.3f %100s %15.0f)) b(%15.3f) starlevels(* 0.1 ** 0.05 *** 0.01) nonotes" 

* For multi-column headers
global multicolumn "prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})"

/*--------------------------------------
------- 2. Retrieving estimates  -------
----------------------------------------*/

filelist , dir($Results) pattern(*posneg_cat*.ster)
levelsof filename, local(results)

foreach r in `results' {
	estimates use "$Results/`r'"
	
	* Adding additional statistics
	if "`r'"!="Table_actual_first.ster"  {
		estadd local countryyear_FE "Yes"
		estadd local grid_FE "Yes"
	}
	
	if "`r'"=="Table_actual_first.ster"  {
		estadd local countryyear_FE "No"
		estadd local grid_FE "No"
		estadd local country_FE "Yes"
		estadd local year_FE "Yes"
	}
	
	if strpos("`r'", "actual")>0 &  "`r'"!="Table_actual_first.ster" {
		estadd local country_FE "No"
		estadd local year_FE "No"
	}
	
	
	* Line indicating sample for main results Table
	if "`r'"=="Table_main_SNL.ster" |  "`r'"=="Table_rob_Berman_or.ster" | "`r'"=="Table_rob_large_Berman" {
		estadd local sample "All"
	}
	
	if "`r'"=="Table_rob_endo1.ster" | "`r'"=="Table_rob_endo1_a.ster" {
	    estadd local sample "AGO, DRC"
	}
	
	if "`r'"=="Table_rob_endo2.ster" | "`r'"=="Table_rob_endo2_a.ster" {
	    estadd local sample "Combined $>$10\%"
	}
	
	if "`r'"=="Table_rob_endo1c.ster" | "`r'"=="Table_rob_endo1c_a.ster" {
	    estadd local sample "AGO, DRC"
	}					
	
	if "`r'"=="Table_rob_endo2c.ster" | "`r'"=="Table_rob_endo2c_a.ster" {
			estadd local sample "Combined $>$10\%"
	}
	
	if strpos("`r'", "actual")>0 {
		estadd local sample "DRC, TZA"
	}

	
	if strpos("`r'", "Table_rob_endo")==0 & "`r'"!="Table_main_SNL.ster" & "`r'"!="Table_rob_Berman_or.ster" & "`r'"!="Table_rob_large_Berman" & strpos("`r'", "actual")==0 {
		estadd local sample "\textbackslash w geo data"
	}
	
	
	* Effect size
	if strpos("`r'", "rob_large")>0 | strpos("`r'", "rob_Berman")>0 {
		use "$Data/main_largegrid.dta", clear
	}
	
	else {
		use "$Data/main.dta", replace
	}
	
	if "`r'"!="Table_actual_first.ster" & strpos("`r'", "het_posneg_cat")==0  {
	
	local depvar = e(depvar)
	local indepvars : colfullnames e(b)
	local count = 0
	foreach var in `indepvars' {
		if `count' == 0 {
			local condition = "`var'!=."
			local sumeff = "e(`var')"
		}
		if `count' != 0 {
			
			local condition = "`condition' & `var'!=."
			local sumeff = "`sumeff' + e(`var')"
		}
		local count = `count'+1
	}
	
	
	
	sum `depvar' if asm_voted==0 & snl_dummy==0 & `condition'

	local controlmean: di %15.3f r(mean)
	local controlsd = r(sd)
	estadd local controlmean `controlmean'
	local sum = 0 
	
	di "`indepvars'"
	foreach var in `indepvars' {
		
		if strpos("`var'", "asmprice")>0 {
			local coef = _b[`var']*524
			local ef_`var': di %15.3f `coef'/`controlsd'/100
			di "`ef_`var'''"
		}
		
		if strpos("`var'", "asmprice")==0 {
			local coef = _b[`var']*404
			local ef_`var': di %15.3f `coef'/`controlsd'/100
			di "`ef_`var'''"
		}
		
		
		if "`r'"!="Table_main_sameASM.ster"  { 
			local sum: di %15.3f `sum' + `ef_`var''
		}
	
	}
	
	if "`r'"=="Table_main_sameASM.ster" {
			local sum: di %15.3f `ef_snl_price' + `ef_onlyasm_price' + `ef_snl_sameasm_price'
	}
	
	estadd local effectsize `sum'
	
	}
	
	* Storing
	local name = subinstr("`r'", ".ster", "", .)
	estimates store `name'
	
}

/*---------------------------
------- 3. Labelling  -------
-----------------------------*/

*NB some labels are different to dataset variable labels to allow for nicer tables

use "$Data/main.dta", clear

label var acled_indicator "acled$>$0"
label var acled_reb "acled$>$0"
label var acled_noreb "acled$>$0"
label var snl_price "ln LSM price $\times$ LSM"
label var asm_price "ln ASM price $\times$ ASM"
label var snl_asm_price "ln LSM price $\times$ LSM $\times$ ASM"
label var snl_asm_asmprice "ln ASM price $\times$ LSM $\times$ ASM"
label var snl_diffasm_asmprice "ln ASM price $\times$ LSM $\times$ diff. ASM"
label var snl_diffasm_lsmprice "ln LSM price $\times$ LSM $\times$ diff. ASM"
label var snl_sameasm_price "ln price $\times$ LSM $\times$ same ASM"

label var price_snl_w "ln w.avg. LSM price $\times$ LSM"
label var asm_price_pred_asm_w "ln w.avg. ASM price $\times$ ASM"
label var snl_asm_price_w "ln w.avg. LSM price $\times$ LSM $\times$ ASM"
label var snl_asm_price_pred_asm_w "ln w.avg. ASM price $\times$ LSM $\times$ ASM"
label var snl_diffasm_asmprice_w "ln w.avg. ASM price $\times$ LSM $\times$ diff. ASM"
label var snl_diffasm_lsmprice_w "ln w.avg. LSM price $\times$ LSM $\times$ diff. ASM"
label var snl_sameasm_price_w "ln w.avg. price $\times$ LSM $\times$ same ASM"

label var price_snl_shock "LSM price shock $\times$ LSM"
label var price_asm_shock "ASM price shock $\times$ ASM"
label var snl_asm_price_shock "LSM price shock $\times$ LSM $\times$ ASM"
label var snl_asm_asmprice_shock "ASM price shock $\times$ LSM $\times$ ASM"
label var snl_diffasm_asmprice_shock "ASM price shock $\times$ LSM $\times$ diff. ASM"
label var snl_diffasm_lsmprice_shock "LSM price shock $\times$ LSM $\times$ diff. ASM"
label var snl_sameasm_price_shock "price shock $\times$ LSM $\times$ same ASM"

label var owner_known_price "ln LSM price $\times$ LSM $\times$ non-miss"
label var owner_known_asm_price "ln LSM price $\times$ LSM $\times$ ASM $\times$ non-miss"
label var owner_local_price "ln LSM price $\times$ LSM $\times$ non-miss \& ..."
label var owner_local_asm_price "ln LSM price $\times$ LSM $\times$ ASM $\times$ non-miss \& ..."

label var op_known_price "ln LSM price $\times$ LSM $\times$ non-miss"
label var op_known_asm_price "ln LSM price $\times$ LSM $\times$ ASM $\times$ non-miss"
label var op_local_price "ln LSM price $\times$ LSM $\times$ non-miss \& ..."
label var op_local_asm_price "ln LSM price $\times$ LSM $\times$ ASM $\times$ non-miss \& ..."

label var owner_known_asm_price_a "ln ASM price $\times$ LSM $\times$ ASM $\times$ non-miss"
label var owner_local_asm_price_a "ln ASM price $\times$ LSM $\times$ ASM $\times$ non-miss \& ..."
label var op_known_asm_price_a "ln ASM price $\times$ LSM $\times$ ASM $\times$ non-miss"
label var op_local_asm_price_a "ln ASM price $\times$ LSM $\times$ ASM $\times$ non-miss \& ..."

label var snl_price_plac "ln LSM placebo $\times$ LSM"
label var asm_price_plac "ln ASM placebo $\times$ ASM"
label var snl_asm_price_plac "ln LSM placebo $\times$ LSM $\times$ ASM"
label var snl_asm_asmprice_plac "ln ASM placebo $\times$ LSM $\times$ ASM"

label var snl_asm_s_price  "ln LSM price $\times$ LSM $\times$ ASM"
label var asm_s_price_asm "ln average ASM price $\times$ ASM"
label var asm_s_price_gold "ln gold price $\times$ ASM"
label var asm_s_price_modal "ln pred. price $\times$ ASM"
label var asm_s_price_modal_prob "ln w. pred. price $\times$ ASM"
label var snl_asm_s_price_asm "ln average ASM price $\times$ LSM $\times$ ASM"
label var snl_asm_s_price_gold "ln gold price $\times$ LSM $\times$ ASM"
label var snl_asm_s_price_modal "ln pred. price $\times$ LSM $\times$ ASM"
label var snl_asm_s_price_modal_prob "ln w. pred. price $\times$ LSM $\times$ ASM"

label var snl_price_chneg "ln LSM price $\times$ LSM $\times$ neg. shock"
label var snl_price_chpos "ln LSM price $\times$ LSM $\times$ pos. shock"
label var asm_price_chneg "ln ASM price $\times$ ASM $\times$ neg. shock"
label var asm_price_chpos "ln ASM price $\times$ ASM $\times$ pos. shock"
label var snl_asm_price_chneg "ln LSM price $\times$ LSM $\times$ ASM $\times$ neg. shock"
label var snl_asm_price_chpos "ln LSM price $\times$ LSM $\times$ ASM $\times$ pos. shock"
label var snl_asm_asmprice_chneg "ln ASM price $\times$ LSM $\times$ ASM $\times$ neg. shock"
label var snl_asm_asmprice_chpos "ln ASM price $\times$ LSM $\times$ ASM $\times$ pos. shock"

label var snlmulti_price "ln LSM price $\times$ multi-LSM" 
label var snlmulti_asm_price "ln LSM price $\times$ multi-LSM $\times$ ASM" 
label var snlmulti_asm_asmprice "ln ASM price $\times$ multi-LSM $\times$ ASM" 

label var near_snl_price "ln LSM price_{i+1} $\times$ LSM_{i+1}"
label var near_asm_price "ln ASM price_{i+1} $\times$ ASM_{i+1}"
label var near_snl_asm_price "ln LSM price_{i+1} $\times$ LSM_{i+1} $\times$ ASM_{i+1}"
label var near_snl_asm_asmprice "ln ASM price_{i+1} $\times$ LSM_{i+1} $\times$ ASM_{i+1}"

label var actual_asm_price "ln ASM price $\times$ actual ASM"
label var snl_asm_actual_price "ln LSM price $\times$ LSM $\times$ actual ASM"
label var asm_truepos_price_voted "ln ASM price $\times$ true pos. ASM"
label var snl_asm_truepos_price "ln LSM price $\times$ LSM $\times$ true pos. ASM"
label var asm_falsepos_price "ln ASM price $\times$ false pos. ASM"
label var snl_asm_falsepos_price "ln LSM price $\times$ LSM $\times$ false pos. ASM"
label var asm_falseneg_price "ln ASM price $\times$ false neg. ASM"
label var snl_asm_falseneg_price "ln LSM price $\times$ LSM $\times$ false neg. ASM"
label var snl_asm_truepos_asmprice_v "ln ASM price $\times$ LSM $\times$ true pos. ASM"
label var snl_asm_falsepos_asmprice "ln ASM price $\times$ LSM $\times$ false pos. ASM"
label var snl_asm_falseneg_asmprice "ln ASM price $\times$ LSM $\times$ false neg. ASM"
label var asm_voted "ASM suitability"
label var actual_asm "actual ASM"

foreach X in snl asm both {
	if "`X'"=="snl" {
		local label = "LSM_i"		
	}
	if "`X'"=="asm" {
		local label = "ASM_i"		
	}
	if "`X'"=="both" {
		local label = "LSM_i $\times$ ASM_i"		
	}
	
	label var near`X'_snl "ln LSM price_{i+1} $\times$ LSM_{i+1}"
	label var near`X'_asm "ln ASM price_{i+1} $\times$ ASM_{i+1}"
	label var near`X'_snl_asm "ln LSM price_{i+1} $\times$ LSM_{i+1} $\times$ ASM_{i+1}"
	
	label var near`X'_snl_asm_asmprice "ln ASM price_{i+1} $\times$ LSM_{i+1} $\times$ ASM_{i+1}"
}

foreach X in gold 3t 2c diamond {
	
	local label = subinstr("`X'", "comm_", "", .)
	

	label var asm`X'_lsmprice "ln ASM price $\times$ `label' ASM "
	label var asm`X'_asmprice "ln ASM price $\times$ `label' ASM" 
	label var snl_asm`X'_lsmprice "ln LSM price $\times$ LSM $\times$ `label' ASM "
	label var snl_asm`X'_asmprice "ln ASM price $\times$ LSM $\times$ `label' ASM "

}


/*-----------------------------------------------
------- 4. Creating Tables for main text  -------
-------------------------------------------------*/

*************
** Table 3 **
*************

*** Main results 
esttab Table_main_SNL Table_main_geosample Table_main Table_main_ASMprice Table_main_sameASM using "$Tables/Table_mainresults.tex", $tabledesign rename(onlyasm_price asm_price) order(snl_price asm_price) 


*************
** Table 4 **
*************

** Heterogeneity by rebel involvement
esttab Table_het_acled_noreb Table_het_acled_noreb_a Table_het_acled_reb Table_het_acled_reb_a  using "$Tables/Table_het_reb.tex", $tabledesign /// 
mgroups("Without non-state armed actors" "With non-state armed actors", pattern(1 0 1 0) $multicolumn) 

*************
** Table 5 **
*************

** Heterogeneity by company type
* LSM*ASM price is LSM price 
* For owners
esttab Table_het_owner_isin Table_het_owner_tax Table_het_owner_local Table_het_owner_chn Table_het_owner_oecd9 using "$Tables/Table_het_comp.tex", $tabledesign_noeff rename(owner_tax_price owner_local_price owner_tax_asm_price owner_local_asm_price owner_chn_price owner_local_price owner_chn_asm_price owner_local_asm_price owner_oecd9_price owner_local_price owner_oecd9_asm_price owner_local_asm_price owner_isin_price owner_local_price owner_isin_asm_price owner_local_asm_price owner_c_kn_price owner_known_price owner_c_kn_asm_price owner_known_asm_price owner_large_price owner_local_price owner_large_asm_price owner_local_asm_price) order(snl_price owner_known_price owner_local_price asm_price snl_asm_price owner_known_asm_price owner_local_asm_price) mlabels("public" "tax haven" "local" "Chinese" "OECD+9")

/*-----------------------------------------------
------- 5. Creating Tables for appendix  --------
-------------------------------------------------*/

**************
** Table C1 **
**************

** Heterogeneity by company type
* For operators 
* LSM*ASM cells have LSM price 
esttab Table_het_op_isin Table_het_op_local Table_het_op_chn Table_het_op_oecd9 using "$Tables/Table_het_comp_op.tex", $tabledesign_noeff rename(op_tax_price op_local_price op_tax_asm_price op_local_asm_price op_chn_price op_local_price op_chn_asm_price op_local_asm_price op_oecd9_price op_local_price op_oecd9_asm_price op_local_asm_price op_isin_price op_local_price op_isin_asm_price op_local_asm_price op_c_kn_price op_known_price op_c_kn_asm_price op_known_asm_price) order(snl_price op_known_price op_local_price asm_price snl_asm_price op_known_asm_price op_local_asm_price) mlabels("public" "local" "Chinese" "OECD+9")

**************
** Table C2 **
**************

* For owners
* LSM*ASM cells have ASM price 
esttab Table_het_owner_isin_a Table_het_owner_tax_a Table_het_owner_local_a Table_het_owner_chn_a Table_het_owner_oecd9_a using "$Tables/Table_het_comp_a.tex", $tabledesign_noeff rename(owner_tax_price owner_local_price owner_tax_asm_price_a owner_local_asm_price_a owner_chn_price owner_local_price owner_chn_asm_price_a owner_local_asm_price_a owner_oecd9_price owner_local_price owner_oecd9_asm_price_a owner_local_asm_price_a owner_isin_price owner_local_price owner_isin_asm_price_a owner_local_asm_price_a owner_c_kn_price owner_known_price owner_c_kn_asm_price_a owner_known_asm_price_a) order(snl_price owner_known_price owner_local_price asm_price snl_asm_asmprice owner_known_asm_price_a owner_local_asm_price_a) mlabels("public" "tax haven" "local" "Chinese" "OECD+9")

**************
** Table C3 **
**************

* For operators
* LSM*ASM cells have ASM price 
esttab Table_het_op_isin_a Table_het_op_local_a Table_het_op_chn_a Table_het_op_oecd9_a using "$Tables/Table_het_comp_op_a.tex", $tabledesign_noeff rename(op_tax_price op_local_price op_tax_asm_price_a op_local_asm_price_a op_chn_price op_local_price op_chn_asm_price_a op_local_asm_price_a op_oecd9_price op_local_price op_oecd9_asm_price_a op_local_asm_price_a op_isin_price op_local_price op_isin_asm_price_a op_local_asm_price_a op_c_kn_price op_known_price op_c_kn_asm_price_a op_known_asm_price_a) order(snl_price op_known_price op_local_price asm_price snl_asm_asmprice op_known_asm_price_a op_local_asm_price_a) mlabels("public" "local" "Chinese" "OECD+9")

**************
** Table C4 **
**************

** Company heterogeneity by size 
* LSM*ASM cells have LSM price 
esttab Table_het_owner_size Table_het_owner_large Table_het_op_size Table_het_op_large using "$Tables/Table_het_comp_size.tex", $tabledesign_noeff rename(owner_size_price owner_local_price owner_size_asm_price owner_local_asm_price owner_large_price owner_local_price owner_large_asm_price owner_local_asm_price op_size_price owner_local_price op_size_asm_price owner_local_asm_price op_large_price owner_local_price op_large_asm_price owner_local_asm_price op_known_price owner_known_price op_large_price owner_local_price op_large_asm_price owner_local_asm_price op_size_asm_price owner_local_asm_price op_known_asm_price owner_known_asm_price) order(snl_price owner_known_price owner_local_price asm_price snl_asm_price owner_known_asm_price owner_local_asm_price) mgroups(Owner Operator, pattern(1 0 1 0) $multicolumn) mlabels("size" "large" "size" "large") 

**************
** Table C5 **
**************

* LSM*ASM cells have ASM price 
esttab Table_het_owner_size_a Table_het_owner_large_a Table_het_op_size_a Table_het_op_large_a using "$Tables/Table_het_comp_size_a.tex", $tabledesign_noeff rename(owner_size_price owner_local_price owner_size_asm_price_a owner_local_asm_price_a owner_large_price owner_local_price owner_large_asm_price_a owner_local_asm_price_a op_size_price owner_local_price op_size_asm_price_a owner_local_asm_price_a op_large_price owner_local_price op_large_asm_price_a owner_local_asm_price_a op_known_price owner_known_price op_large_price owner_local_price op_large_asm_price_a owner_local_asm_price_a op_size_asm_price_a owner_local_asm_price_a op_known_asm_price_a owner_known_asm_price_a) order(snl_price owner_known_price owner_local_price asm_price snl_asm_asmprice owner_known_asm_price_a owner_local_asm_price_a) mgroups(Owner Operator, pattern(1 0 1 0) $multicolumn) mlabels("size" "large" "size" "large") 

**************
** Table C8 **
**************

** Interaction with known name and country known only 
* LSM*ASM cells have LSM price 
esttab Table_het_owner_known Table_het_owner_c_kn Table_het_op_known Table_het_op_c_kn using "$Tables/Table_het_comp_known.tex", $tabledesign_noeff rename(owner_c_kn_price owner_known_price owner_c_kn_asm_price owner_known_asm_price op_known_price owner_known_price op_known_asm_price owner_known_asm_price op_c_kn_price owner_known_price op_c_kn_asm_price owner_known_asm_price) order(snl_price owner_known_price asm_price snl_asm_price owner_known_asm_price) mgroups(Owner Operator, pattern(1 0 1 0) $multicolumn) mlabels("name" "country" "name" "country") 

**************
** Table C9 **
**************

* LSM*ASM cells have ASM price 
esttab Table_het_owner_known_a Table_het_owner_c_kn_a Table_het_op_known_a Table_het_op_c_kn_a using "$Tables/Table_het_comp_known_a.tex", $tabledesign_noeff rename(owner_c_kn_price owner_known_price owner_c_kn_asm_price_a owner_known_asm_price_a op_known_price owner_known_price op_known_asm_price_a owner_known_asm_price_a op_c_kn_price owner_known_price op_c_kn_asm_price_a owner_known_asm_price_a) order(snl_price owner_known_price asm_price snl_asm_asmprice owner_known_asm_price_a) mgroups(Owner Operator, pattern(1 0 1 0) $multicolumn) mlabels("name" "country" "name" "country") 

** Controlling for size 
* Relabelling 

foreach X in owner op {
label var `X'_known_price "ln LSM price $\times$ LSM $\times$ named"
label var `X'_known_asm_price "ln LSM price $\times$ LSM $\times$ ASM $\times$ named"
label var `X'_size_price "ln LSM price $\times$ LSM $\times$ named $\times$ size"
label var `X'_size_asm_price "ln LSM price $\times$ LSM $\times$ ASM $\times$ named $\times$ size"
label var `X'_isin_asm_price "ln LSM price $\times$ LSM $\times$ ASM $\times$ named $\times$ public"
label var `X'_isin_price "ln LSM price $\times$ LSM $\times$ named $\times$ public"
label var `X'_c_kn_price "ln LSM price $\times$ LSM $\times$ \textbackslash w country"
label var `X'_c_kn_asm_price "ln LSM price $\times$ LSM $\times$ ASM $\times$ \textbackslash w country"
label var `X'_local_price "ln LSM price $\times$ LSM $\times$ \textbackslash w country \& ..."
label var `X'_local_asm_price "ln LSM price $\times$ LSM $\times$ ASM $\times$ \textbackslash w country \& ..."
}

**************
** Table C6 **
**************

* Owner 
* LSM*ASM cells have LSM price
esttab Table_het_owner_isin_s Table_het_owner_tax_s Table_het_owner_local_s Table_het_owner_chn_s Table_het_owner_oecd9_s using "$Tables/Table_het_comp_s.tex",  $tabledesign_noeff rename(owner_tax_price owner_local_price owner_tax_asm_price owner_local_asm_price owner_chn_price owner_local_price owner_chn_asm_price owner_local_asm_price owner_oecd9_price owner_local_price owner_oecd9_asm_price owner_local_asm_price) order(snl_price owner_known_price owner_c_kn_price owner_size_price owner_isin_price owner_local_price asm_price snl_asm_price owner_known_asm_price owner_c_kn_asm_price owner_size_asm_price owner_isin_asm_price owner_local_asm_price) mlabels("public" "tax haven" "local" "Chinese" "OECD+9")

**************
** Table C7 **
**************

* Operator 
* LSM*ASM cells have LSM price
esttab Table_het_op_isin_s Table_het_op_local_s Table_het_op_chn_s Table_het_op_oecd9_s using "$Tables/Table_het_comp_op_s.tex",  $tabledesign_noeff rename(op_tax_price op_local_price op_tax_asm_price op_local_asm_price op_chn_price op_local_price op_chn_asm_price op_local_asm_price op_oecd9_price op_local_price op_oecd9_asm_price op_local_asm_price) order(snl_price op_known_price op_c_kn_price op_size_price op_isin_price op_local_price asm_price snl_asm_price op_known_asm_price op_c_kn_asm_price op_size_asm_price op_isin_asm_price op_local_asm_price) mlabels("public" "local" "Chinese" "OECD+9")

***************
** Table C10 **
***************

*** Heterogeneity by commodity
esttab Table_het_comm_lsmprice Table_het_comm_asmprice using "$Tables/Table_het_commodity.tex", $tabledesign_noeff  rename(asmgold_asmprice asmgold_lsmprice asm2c_asmprice asm2c_lsmprice asm3t_asmprice asm3t_lsmprice asmdiamond_asmprice asmdiamond_lsmprice)

***************
** Table C11 **
***************

** Heterogeneity by event type

* LSM*ASM price is LSM price
esttab Table_het_battle Table_het_civ Table_het_civ_gov Table_het_civ_reb Table_het_protest_peace Table_het_riot using "$Tables/Table_het_event.tex", $tabledesign ///
 mgroups(Battle Civilians Protest, pattern(1 1 0 0 1 0) $multicolumn) mlabels("all" "all" "gov" "reb" "non-viol." "riot") 

***************
** Table C12 **
***************
 
* LSM*ASM price is ASM price
esttab Table_het_battle_a Table_het_civ_a Table_het_civ_gov_a Table_het_civ_reb_a Table_het_protest_peace_a Table_het_riot_a using "$Tables/Table_het_event_a.tex", $tabledesign ///
 mgroups(Battle Civilians Protest, pattern(1 1 0 0 1 0) $multicolumn) mlabels("all" "all" "gov" "reb" "non-viol." "riot") 

***************
** Table C13 **
***************

** Heterogeneity by upward and downward price shocks
esttab Table_het_posneg_bin Table_het_posneg_bin_a using "$Tables/Table_het_updown.tex", $tabledesign_noeff order(snl_price_chneg snl_price_chpos asm_price_chneg asm_price_chpos snl_asm_price_chneg snl_asm_price_chpos snl_asm_asmprice_chneg snl_asm_asmprice_chpos)

***************
** Table C14 **
***************

** Multiple LSM mines
esttab Table_het_multi Table_het_multi_a using "$Tables/Table_het_multi.tex", $tabledesign_noeff

***************
** Table C15 **
***************

** Spatial lags 
esttab Table_het_splag Table_het_splag_a using "$Tables/Table_het_splag.tex", $tabledesign_noeff order(snl_price asm_price snl_asm_price snl_asm_asmprice near_snl_price near_asm_price near_snl_asm_price near_snl_asm_asmprice) substitute("\_{i+1}" "_{i+1}")

***************
** Table D1 ***
***************

** Main results with stable sample 
esttab Table_rob_geosample_samp Table_rob_samp Table_rob_ASMprice_samp Table_main_sameASM using "$Tables/Table_rob_samp.tex", $tabledesign rename(onlyasm_price asm_price) order(snl_price asm_price) 

***************
** Table D2 ***
***************

** Endogenous and placebo prices 
* LSM*ASM price is LSM price 
esttab Table_rob_endo1 Table_rob_endo2 Table_rob_endo1c Table_rob_endo2c Table_rob_plac using "$Tables/Table_rob_endo.tex", $tabledesign mgroups("Drop commodity-cell" "Drop country" "", pattern(1 0 1 0 1) $multicolumn)

***************
** Table D3 ***
***************

* LSM*ASM price is ASM price
esttab Table_rob_endo1_a Table_rob_endo2_a Table_rob_endo1c_a Table_rob_endo2c_a Table_rob_plac_a using "$Tables/Table_rob_endo_a.tex", $tabledesign mgroups("Drop commodity-cell" "Drop country" "", pattern(1 0 1 0 1) $multicolumn)

***************
** Table D4 ***
***************

** Weighted average resource price  
esttab Table_main_SNL_w Table_main_geosample_w Table_main_w Table_main_ASMprice_w Table_main_sameASM_w using "$Tables/Table_rob_price_w.tex", $tabledesign rename(onlyasm_price_w asm_price_pred_asm_w) order(price_snl_w asm_price_pred_asm_w) 

***************
** Table D5 ***
***************

** Maximum price shock 
esttab Table_main_SNL_shock Table_main_geosample_shock Table_main_shock Table_main_ASMprice_shock Table_main_sameASM_shock using "$Tables/Table_rob_price_shock.tex", $tabledesign rename(onlyasm_price_shock price_asm_shock) order(price_snl_shock price_asm_shock) 

***************
** Table D6 ***
***************

** Single ASM model 
* LSM*ASM price is LSM price 
esttab T_rob_asm_s T_rob_gold_s T_rob_modal_s T_rob_modal_prob_s using "$Tables/Table_rob_singlemodel.tex", $tabledesign order(snl_price asm_s_price_asm asm_s_price_gold asm_s_price_modal asm_s_price_modal_prob)

***************
** Table D7 ***
***************

* LSM*ASM price is ASM price 
esttab T_rob_asm_as T_rob_gold_as T_rob_modal_as T_rob_modal_prob_as using "$Tables/Table_rob_singlemodel_a.tex", $tabledesign order(snl_price asm_s_price_asm asm_s_price_gold asm_s_price_modal asm_s_price_modal_prob) mgroups("Commodity-specific ML model" "Single ML model", pattern(1 0 1 0) $multicolumn)

***************
** Table D8 ***
***************

** Real ASM data 
esttab Table_actual_first Table_actual_main Table_actual_pos_lsm_1 Table_actual_pos_asm_1 using "$Tables/Table_rob_actual.tex", order(asm_voted snl_price asm_price snl_asm_price *truepos* *falsepos* *falseneg*) $tabledesign_actual

***************
** Table D9 ***
***************

** Larger cell size 
esttab Table_rob_large_Berman Table_rob_large_samp Table_rob_large Table_rob_large_ASMprice using "$Tables/Table_largecells.tex", $tabledesign order(snl_price asm_price) 


/*---------------------------------
------- 6. Creating figures  ------
-----------------------------------*/

*****************
*** Figure C1 ***
*****************

preserve
foreach var of varlist _all {
	local label : variable label `var'
	local label = subinstr("`label'", "$\times$", "x", .)
	label var `var' "`label'"
} 

graph set window fontface cambria

* LSMxASM is LSM price
coefplot Table_het_comm_lsmprice, /// 
plotregion(style(none)) graphregion(ifcolor(white) fcolor(white) color(white) icolor(white)) /// 
xline(0, lcolor(red)) mcolor(black) ciopts(lcolor(black)) ///
headings(asmgold_lsmprice = "{bf:ln ASM price x ASM}" snl_asmgold_lsmprice = "{bf:ln ASM price x LSM x ASM}") /// 
xtitle(coefficient) 

graph export "$Figures/het_commodity.pdf", replace

*****************
*** Figure C2 ***
*****************

* LSMxASM is ASM price
coefplot Table_het_comm_asmprice, /// 
plotregion(style(none)) graphregion(ifcolor(white) fcolor(white) color(white) icolor(white)) /// 
xline(0, lcolor(red)) mcolor(black) ciopts(lcolor(black)) ///
headings(asmgold_asmprice = "{bf:ln LSM price x ASM}" snl_asmgold_asmprice = "{bf:ln ASM price x LSM x ASM}") /// 
xtitle(coefficient) 
 
graph export "$Figures/het_commodity_a.pdf", replace

restore

*****************
*** Figure C3 ***
*****************

foreach i in 50 25 {
    
	label var snl_price_chneg`i' "- `i'%"
	label var asm_price_chneg`i' "- `i'%"
	label var snl_asm_price_chneg`i' "- `i'%"
	label var snl_asm_asmprice_chneg`i' "- `i'%"
}

foreach i in 125 15 175 2 {
    if "`i'"=="125" {
	    local j = 25
	}
	if "`i'"=="15" {
	    local j = 50
	}
    if "`i'"=="175" {
	    local j = 75
	}
	if "`i'"=="2" {
	    local j = 100
	}
	label var snl_price_ch`i' "+ `j'%"
	label var asm_price_ch`i' "+ `j'%"
	label var snl_asm_price_ch`i' "+ `j'%"
	label var snl_asm_asmprice_ch`i' "+ `j'%"
	
}

* LSMxASM is LSM price
coefplot Table_het_posneg_cat, ///
plotregion(style(none)) graphregion(ifcolor(white) fcolor(white) color(white) icolor(white)) /// 
drop(snl_price_ch asm_price_ch snl_asm_price_ch) /// 
xline(0, lcolor(red)) mcolor(black) ciopts(lcolor(black)) ///
headings(snl_price_chneg50 = "{bf:ln LSM price x LSM x}" asm_price_chneg50 = "{bf:ln ASM price x ASM x}" snl_asm_price_chneg50 = "{bf:ln LSM price x LSM x ASM x}") /// 
xtitle(coefficient) 

graph export "$Figures/het_posneg.pdf", replace

*****************
*** Figure C4 ***
*****************

* LSMxASM is ASM price
coefplot Table_het_posneg_cat_a, ///
plotregion(style(none)) graphregion(ifcolor(white) fcolor(white) color(white) icolor(white)) /// 
drop(snl_price_ch asm_price_ch snl_asm_asmprice_ch) /// 
xline(0, lcolor(red)) mcolor(black) ciopts(lcolor(black)) ///
headings(snl_price_chneg50 = "{bf:ln LSM price x LSM x}" asm_price_chneg50 = "{bf:ln ASM price x ASM x}" snl_asm_asmprice_chneg50 = "{bf:ln ASM price x LSM x ASM x}") /// 
xtitle(coefficient) 

graph export "$Figures/het_posneg_a.pdf", replace

*****************
*** Figure C5 ***
*****************

preserve
foreach var of varlist _all {
	local label : variable label `var'
	local label = subinstr("`label'", "$\times$", "x", .)
	local label = subinstr("`label'", "_{i+1}", "{sub:i+1}", .)
	label var `var' "`label'"
} 

coefplot Table_het_splag_full, /// 
plotregion(style(none)) graphregion(ifcolor(white) fcolor(white) color(white) icolor(white)) /// 
xline(0, lcolor(red)) mcolor(black) ciopts(lcolor(black)) ///
headings(near_snl_price = "{bf:Cell i has no mining}" nearsnl_snl = "{bf:Cell i has LSM only}" nearasm_snl = "{bf:Cell i has ASM only}" nearboth_snl = "{bf:Cell i has LSM and ASM}") /// 
xtitle(coefficient) 

graph export "$Figures/het_splag.pdf", replace

*****************
*** Figure C6 ***
*****************

coefplot Table_het_splag_full_a, /// 
plotregion(style(none)) graphregion(ifcolor(white) fcolor(white) color(white) icolor(white)) /// 
xline(0, lcolor(red)) mcolor(black) ciopts(lcolor(black)) ///
headings(near_snl_price = "{bf:Cell i has no mining}" nearsnl_snl = "{bf:Cell i has LSM only}" nearasm_snl = "{bf:Cell i has ASM only}" nearboth_snl = "{bf:Cell i has LSM and ASM}") /// 
xtitle(coefficient) 

graph export "$Figures/het_splag_a.pdf", replace

*****************
*** Figure D1 ***
*****************

matrix actual = J(7, 6, .)

* Using LSM prices for triple interaction cells and predicted prices for true positive cells 
my_reg2hdfespatial acled_indicator snl_price asm_truepos_price_voted snl_asm_truepos_price asm_falsepos_price snl_asm_falsepos_price asm_falseneg_price snl_asm_falseneg_price if gid_0!="BFA", timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000)

global combos "snl_price asm_truepos_price_voted asm_falsepos_price asm_falseneg_price snl_price+asm_truepos+snl_asm_truepos_price  snl_price+asm_falsepos_price+snl_asm_falsepos_price  snl_price+asm_falseneg_price+snl_asm_falseneg_price"

local count = 0 

foreach C in $combos {
	
	local count = `count'+1
	
	lincom `C'
	
	local estimate = r(estimate)
	matrix actual[`count', 1]=`estimate'
	
	local lb = r(lb)
	matrix actual[`count', 2]=`lb'
	
	local ub = r(ub)
	matrix actual[`count', 3]=`ub'
	
}

* Using ASM prices for triple interaction cells and predicted prices for true positive cells 
my_reg2hdfespatial acled_indicator snl_price asm_truepos_price_voted snl_asm_truepos_asmprice_v asm_falsepos_price snl_asm_falsepos_asmprice asm_falseneg_price snl_asm_falseneg_asmprice if gid_0!="BFA", timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000)

global combos_asmprice "snl_price asm_truepos_price_voted asm_falsepos_price asm_falseneg_price  snl_price+asm_truepos+snl_asm_truepos_asmprice_v snl_price+asm_falsepos_price+snl_asm_falsepos_asmprice snl_price+asm_falseneg_price+snl_asm_falseneg_asmprice"

local count = 0 

foreach C in $combos_asmprice {
	
	local count = `count'+1
	
	lincom `C'
	
	local estimate = r(estimate)
	matrix actual[`count', 4]=`estimate'
	
	local lb = r(lb)
	matrix actual[`count', 5]=`lb'
	
	local ub = r(ub)
	matrix actual[`count', 6]=`ub'
	
}

clear
svmat actual

rename (actual1 actual2 actual3 actual4 actual5 actual6) (lsmprice_est lsmprice_lb lsmprice_ub asmprice_est asmprice_lb asmprice_ub)
gen id = _n

replace id = id+1 if _n==2 | _n==3 | _n==4
replace id = id+2 if _n==5 | _n==6 | _n==7

gen falseposneg = 1 if _n==1 
replace falseposneg = 2 if _n==2 | _n==5
replace falseposneg = 3 if _n==3 | _n==6
replace falseposneg = 4 if _n==4 | _n==7

* LSM x ASM gets LSM price
graph twoway (bar lsmprice_est id if falseposneg==1, lcolor(black) fcolor(black)) ///
(bar lsmprice_est id if falseposneg==2, lcolor(black) fcolor(gs4)) ///
(bar lsmprice_est id if falseposneg==3, lcolor(black) fcolor(gs10)) ///
(bar lsmprice_est id if falseposneg==4, lcolor(black) fcolor(white)) ///
 (rcap lsmprice_lb lsmprice_ub id, lcolor(black)), ///
plotregion(style(none)) graphregion(ifcolor(white) fcolor(white) color(white) icolor(white)) ///
 legend(row(1) order(1 "LSM" 2 "True pos." 3 "False pos." 4 "False neg.") ) ///
xlabel( 1 "LSM price x LSM" 4 "ASM price X ASM" 8 "LSM price x LSM x ASM", noticks) ///
xtitle("") ytitle("Total effect size")
	   
graph export "$Figures/rob_actual.pdf", replace

	   
* LSM x ASM gets ASM price
graph twoway (bar asmprice_est id if falseposneg==1, lcolor(black) fcolor(black)) ///
(bar asmprice_est id if falseposneg==2, lcolor(black) fcolor(gs4)) ///
(bar asmprice_est id if falseposneg==3, lcolor(black) fcolor(gs10)) ///
(bar asmprice_est id if falseposneg==4, lcolor(black) fcolor(white)) ///
 (rcap asmprice_lb asmprice_ub id, lcolor(black)), ///
plotregion(style(none)) graphregion(ifcolor(white) fcolor(white) color(white) icolor(white)) ///
 legend(row(1) order(1 "LSM" 2 "True pos." 3 "False pos." 4 "False neg.") ) ///
       xlabel( 1 "LSM price x LSM" 4 "ASM price X ASM" 8 "ASM price x LSM x ASM", noticks) ///
       xtitle("") ytitle("Total effect size")

graph export "$Figures/rob_actual_a.pdf", replace


*****************
*** Figure B1 ***
*****************

use "$Data/commodityprices.dta", clear

drop main_lprice_new_nominal weight

levelsof mainmineral, local(commodities)

foreach C in `commodities' {
	
	qui sum main_lprice_new if mainmineral=="`C'"
	local sd = r(sd)
	local mean = round(r(mean), 1)
	
	local ymax = round(`mean' + 4*`sd', 1)
	local ymin = round(`mean' - 2*`sd', 1)
	
	if "`C'"=="gold" | "`C'"=="silver" | "`C'"=="platinum" {
		local ylabel = "$/troy ounce"
	}
	if "`C'"=="diamond" {
		local ylabel = "price index"
	}
	if "`C'"!="gold" & "`C'"!="silver" & "`C'"!="platinum" & "`C'"!="diamond" {
		local ylabel = "$/metric ton"
	}
	
	graph twoway line main_lprice_new year if mainmineral=="`C'", ///
	yscale(range(`ymin'(10000)`ymax')) ylabel(`ymin' `mean' `ymax') ytitle(`ylabel') lcolor(black) ///
	plotregion(style(none)) graphregion(ifcolor(white) fcolor(white) color(white) icolor(white)) ///
	title("`C'") xscale(range(1995(5)2020))
	
	graph save "$Figures/price_`C'.gph", replace
}

graph combine ///
"$Figures/price_aluminum" ///
"$Figures/price_coal" ///
"$Figures/price_cobalt" ///
"$Figures/price_coltan" ///
"$Figures/price_copper" ///
"$Figures/price_diamond" ///
"$Figures/price_gold" ///
"$Figures/price_iron" ///
"$Figures/price_lead" ///
"$Figures/price_manganese" ///
"$Figures/price_nickel" ///
"$Figures/price_phosphate" ///
"$Figures/price_platinum" ///
"$Figures/price_silver" ///
"$Figures/price_tin" ///
"$Figures/price_tungsten" ///
"$Figures/price_zinc", /// 
plotregion(style(none)) graphregion(ifcolor(white) fcolor(white) color(white) icolor(white)) 

graph export "$Figures/comm_prices.pdf", replace


/*----------------------------------------
------- 7. Descriptive statistics  -------
------------------------------------------*/

use "$Data/main.dta", replace

*****************
*** Table B2 ****
*****************

* Summary stats
label var acled_indicator "ACLED indicator"
label var acled_reb "ACLED event with non-state armed groups"
label var acled_noreb "ACLED event involving no non-stated armed groups"
label var price_snl "ln LSM price"
label var ln_price_modal_asm "ln ASM price"
label var snl_dummy "LSM"
label var asm_voted "ASM"
label var diffASM "ASM mines different commodity to LSM"
label var sameASM "ASM mines same commodity as LSM"
label var asm_modal_gold "modal ASM commodity is gold"
label var asm_modal_diamond "modal ASM commodity is diamond"
label var asm_modal_2c "modal ASM commodity is copper or cobalt"
label var asm_modal_3t "modal ASM commodity is 3t"
label var owner_isin "Mine owner publicly traded"
label var op_isin "Mine operator publicly traded"
label var owner_tax "Mine owner registered in tax haven"
label var owner_local "Mine owner registered locally"
label var owner_chn "Mine owner registered in China"
label var op_local "Mine operator registered locally"
label var op_chn "Mine operator registered in China"
label var owner_oecd9 "Mine owner registered in OECD+9 country"
label var op_oecd9 "Mine operator registered in OECD+9 country"
label var owner_known "Mine owner named"
label var op_known "Mine operator named"
label var owner_c_kn "Mine owner has non-missing country of registration"
label var op_c_kn "Miner operator has non-missing country of registration"
label var owner_size "Mine owner size (\# of assets owned)"
label var op_size "Miner operator size (\# of assets operated)"
label var owner_large "Miner owner large ($\geq$ 10 assets owned)"
label var op_large "Mine operator large ($\geq$ 10 assets operated)"

replace snl_dummy = 0 if snl_dummy==1 & snl_commodity=="none"

global allvars = "acled_indicator acled_reb acled_noreb price_snl ln_price_modal_asm snl_dummy asm_voted diffASM sameASM asm_modal_gold asm_modal_diamond asm_modal_2c asm_modal_3t owner_isin owner_tax owner_local owner_chn owner_oecd9 owner_known owner_c_kn owner_size owner_large"

estpost summarize $allvars if snl_price!=. & asm_price!=. & snl_asm_price!=.
esttab using "$Tables/summstats_snl.tex", replace cells("count(fmt(0)) mean(fmt(3)) sd(fmt(3)) min max") label nomtitle nonumber noobs

*****************
*** Table B1 ****
*****************
 
use "$Data/commodityprices.dta", clear

drop main_lprice_new_nominal weight
egen mineralID = group(mainmineral)

drop if main_lprice_new==.

reshape wide main_lprice_new mainmineral, i(year) j(mineralID)

forvalues i = 1(1)17 {
	local name = mainmineral`i'[1]
	di "`name'"
	
	rename main_lprice_new`i' price_`name'
	
	drop mainmineral`i'
	
	label var price_`name' "price `name'"
	
}

rename price_ price_diamond
label var price_diamond "price diamond"

estpost summarize price_*
esttab using "$Tables/summstats_prices.tex", replace cells("count mean sd min max") b(%9.3f) label nomtitle nonumber noobs

/*----------------------------------------
------- 8. Table D10		   -----------
------------------------------------------*/

use "$Data/main_largegrid.dta", clear

label var main_lprice_hist0 "ln LSM price $\times$ LSM"
label var price_hist0_asm "ln LSM price $\times$ LSM $\times$ ASM"
label var asmprice_hist0_asm "ln ASM price $\times$ LSM $\times$ ASM"
label var asm_price "ln ASM price $\times$ ASM"

** Berman results 
* Table with only Berman results
esttab Table_rob_Berman_or Table_rob_Berman_samp Table_rob_Berman Table_rob_Berman_ASMprice using "$Tables/Table_Berman.tex", $tabledesign order(main_lprice_hist0 asm_price price_hist0_asm asmprice_hist0_asm)

